package com.pi.wms.service;



import com.pi.common.utils.StringUtils;
import com.pi.framework.core.repository.IBaseRepository;
import com.pi.framework.core.service.impl.BaseCrudService;
import com.pi.wms.entity.TwtActDailyWorkSummary;
import com.pi.wms.repository.DailyWorkSummRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Slf4j
@Transactional
@Service
public class DailyWorkSummService extends BaseCrudService<TwtActDailyWorkSummary, Long>{

	@Autowired
	protected DailyWorkSummRepository repository;

    @Override
	protected IBaseRepository<TwtActDailyWorkSummary,Long> getRepository() {
		return this.repository;
	}
	
	@Override
	protected Predicate toPredicate(Root<TwtActDailyWorkSummary> root, CriteriaQuery<?> cq, CriteriaBuilder cb, Map<String, ?> query) {
		List<Predicate> list = new ArrayList<Predicate>();

		String divisionid = (String) query.get("divisionid");
		if (!StringUtils.isNullOrBlank(divisionid)) {
			list.add(cb.equal(root.get("divisionId"), divisionid));
		}

		String code = (String) query.get("code");
        if (!StringUtils.isNullOrBlank(code)) {
			list.add(cb.equal(root.get("code"), "%"+code.trim()+"%"));
		}

		String transDate = (String) query.get("transDate");
		if (!StringUtils.isNullOrBlank(transDate)) {
		    transDate=transDate.replaceAll("-","");
			list.add(cb.equal(root.get("reportDate"), transDate.trim()));
		}


		Predicate[] p = new Predicate[list.size()];
		return cb.and(list.toArray(p));
	}




}
